from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.app.layout.viewlets.common import ViewletBase
from Acquisition import aq_inner
import datetime
import random


class FooterViewlet(ViewletBase):
    render = ViewPageTemplateFile('tp/footer.pt')

    def get_year_copyright(self):
        today = datetime.date.today()
        self.today=today
        return today.year

    def is_main(self):
        obj=None
        if self.request.has_key('PUBLISHED'):
            obj=self.request['PUBLISHED']
        attr = getattr(obj, "id", None)
        return (obj and attr=='frontpage')

    def get_random_link(self,category,limit):
        alllinks = self.context.portal_catalog.searchResults(portal_type=('Link'),
                                                        Subject=(category)
                                                        )
       
        objects=[]
        for link in alllinks:
            objects.append(link)

        random.shuffle(objects)
        return objects[:limit]
    
    def site_actions(self):
        context = aq_inner(self.context)
        pactions=context.portal_actions.listFilteredActionsFor(context)
        links=[]
        if pactions.has_key('socialnet'):
            links=pactions['socialnet']
        return links